<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>webSocket</title>
  </head>
  <body>
    <button onclick="sendData()">测试发送数据</button>
    <script>
      let ws = new WebSocket("http://localhost:3000");
      ws.onopen = function () {
        console.log("连接成功");
      };
      ws.onmessage = function (msg) {
        console.log(msg.data);
      };
      ws.onerror = function () {
        console.log("连接出错");
      };
      ws.onclose = function () {
        console.log("连接关闭");
      };
    </script>
    <script>
        function sendData() {
            ws.send("这是发送给后台的消息!");
        }
      </script>
    </script>
  </body>
</html>
